/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    tim.c
  * @brief   This file provides code for the configuration
  *          of the TIM instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"

/* USER CODE BEGIN 0 */
static uint16_t bldcm_pulse = 0;
/* USER CODE END 0 */

TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim7;

/* TIM1 init function */
void MX_TIM1_Init(void)
{

  /* USER CODE BEGIN TIM1_Init 0 */

  /* USER CODE END TIM1_Init 0 */

  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};

  /* USER CODE BEGIN TIM1_Init 1 */

  /* USER CODE END TIM1_Init 1 */
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 2-1;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 5600-1;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_SET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  {
    Error_Handler();
  }
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  sBreakDeadTimeConfig.DeadTime = 0;
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM1_Init 2 */

  /* USER CODE END TIM1_Init 2 */
  HAL_TIM_MspPostInit(&htim1);

}
/* TIM3 init function */
void MX_TIM3_Init(void)
{

  /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_HallSensor_InitTypeDef sConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 128-1;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 65535-1;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC1Filter = 0;
  sConfig.Commutation_Delay = 0;
  if (HAL_TIMEx_HallSensor_Init(&htim3, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC2REF;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */

}
/* TIM7 init function */
void MX_TIM7_Init(void)
{

  /* USER CODE BEGIN TIM7_Init 0 */

  /* USER CODE END TIM7_Init 0 */

  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM7_Init 1 */

  /* USER CODE END TIM7_Init 1 */
  htim7.Instance = TIM7;
  htim7.Init.Prescaler = 1680-1;
  htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim7.Init.Period = 50*50-1;
  htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM7_Init 2 */
    HAL_TIM_Base_Start_IT(&htim7);
  /* USER CODE END TIM7_Init 2 */

}

void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
{

  if(tim_pwmHandle->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspInit 0 */

  /* USER CODE END TIM1_MspInit 0 */
    /* TIM1 clock enable */
    __HAL_RCC_TIM1_CLK_ENABLE();
  /* USER CODE BEGIN TIM1_MspInit 1 */

  /* USER CODE END TIM1_MspInit 1 */
  }
}

void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef* timex_hallsensorHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(timex_hallsensorHandle->Instance==TIM3)
  {
  /* USER CODE BEGIN TIM3_MspInit 0 */

  /* USER CODE END TIM3_MspInit 0 */
    /* TIM3 clock enable */
    __HAL_RCC_TIM3_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    /**TIM3 GPIO Configuration
    PC6     ------> TIM3_CH1
    PC7     ------> TIM3_CH2
    PC8     ------> TIM3_CH3
    */
    GPIO_InitStruct.Pin = MOTOR_EA_HU_Pin|MOTOR_EB_HV_Pin|MOTOR_EZ_HW_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    /* TIM3 interrupt Init */
    HAL_NVIC_SetPriority(TIM3_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(TIM3_IRQn);
  /* USER CODE BEGIN TIM3_MspInit 1 */

  /* USER CODE END TIM3_MspInit 1 */
  }
}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM7)
  {
  /* USER CODE BEGIN TIM7_MspInit 0 */

  /* USER CODE END TIM7_MspInit 0 */
    /* TIM7 clock enable */
    __HAL_RCC_TIM7_CLK_ENABLE();

    /* TIM7 interrupt Init */
    HAL_NVIC_SetPriority(TIM7_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(TIM7_IRQn);
  /* USER CODE BEGIN TIM7_MspInit 1 */

  /* USER CODE END TIM7_MspInit 1 */
  }
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(timHandle->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspPostInit 0 */

  /* USER CODE END TIM1_MspPostInit 0 */

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**TIM1 GPIO Configuration
    PA8     ------> TIM1_CH1
    PA9     ------> TIM1_CH2
    PA10     ------> TIM1_CH3
    */
    GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM1_MspPostInit 1 */
      HAL_TIMEx_ConfigCommutEvent(&htim1,TIM_TS_ITR3,TIM_COMMUTATION_SOFTWARE);
      HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
      HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
      HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
      BLDCM_DISABLE_SD();
  /* USER CODE END TIM1_MspPostInit 1 */
  }

}

void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle)
{

  if(tim_pwmHandle->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspDeInit 0 */

  /* USER CODE END TIM1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM1_CLK_DISABLE();
  /* USER CODE BEGIN TIM1_MspDeInit 1 */

  /* USER CODE END TIM1_MspDeInit 1 */
  }
}

void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef* timex_hallsensorHandle)
{

  if(timex_hallsensorHandle->Instance==TIM3)
  {
  /* USER CODE BEGIN TIM3_MspDeInit 0 */

  /* USER CODE END TIM3_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM3_CLK_DISABLE();

    /**TIM3 GPIO Configuration
    PC6     ------> TIM3_CH1
    PC7     ------> TIM3_CH2
    PC8     ------> TIM3_CH3
    */
    HAL_GPIO_DeInit(GPIOC, MOTOR_EA_HU_Pin|MOTOR_EB_HV_Pin|MOTOR_EZ_HW_Pin);

    /* TIM3 interrupt Deinit */
    HAL_NVIC_DisableIRQ(TIM3_IRQn);
  /* USER CODE BEGIN TIM3_MspDeInit 1 */

  /* USER CODE END TIM3_MspDeInit 1 */
  }
}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM7)
  {
  /* USER CODE BEGIN TIM7_MspDeInit 0 */

  /* USER CODE END TIM7_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM7_CLK_DISABLE();

    /* TIM7 interrupt Deinit */
    HAL_NVIC_DisableIRQ(TIM7_IRQn);
  /* USER CODE BEGIN TIM7_MspDeInit 1 */

  /* USER CODE END TIM7_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */
int update_motor_tim = 0;
void stop_pwm_output(void){
    __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,0);
    __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,0);
    __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,0);
    HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_Port,MOTOR_OCNPWM1_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_Port,MOTOR_OCNPWM2_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_Port,MOTOR_OCNPWM3_Pin,GPIO_PIN_RESET);
}
void set_pwm_pulse(uint16_t pulse){
    bldcm_pulse = pulse;
}
void hall_enable(void){
    __HAL_TIM_ENABLE_IT(&htim3,TIM_IT_TRIGGER);
    __HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE);

    HAL_TIMEx_HallSensor_Start(&htim3);

    HAL_TIM_TriggerCallback(&htim3);
}
void hall_disable(void){
    __HAL_TIM_DISABLE_IT(&htim3,TIM_IT_TRIGGER);
    __HAL_TIM_DISABLE_IT(&htim3, TIM_IT_UPDATE);
    HAL_TIMEx_HallSensor_Stop(&htim3);
}
//shun:6->4->5->1->3->2->6
//mi:6->2->3->1->5->4->6
uint8_t get_hall_state(void){
    uint8_t state;
    if(HAL_GPIO_ReadPin(MOTOR_EA_HU_GPIO_Port,MOTOR_EA_HU_Pin) != RESET)
    {
        state |= 0x01U  << 0;
    }
    if(HAL_GPIO_ReadPin(MOTOR_EB_HV_GPIO_Port,MOTOR_EB_HV_Pin) != RESET)
    {
        state |= 0x02U  << 0;
    }
    if(HAL_GPIO_ReadPin(MOTOR_EZ_HW_GPIO_Port,MOTOR_EZ_HW_Pin) != RESET)
    {
        state |= 0x04U  << 0;
    }
    return state;
}
motor_rotate_t motor_drive = {0};
static uint8_t count = 0;
static void update_speed_dir(uint8_t dir_in){
    uint8_t step[6] = {1,3,2,6,4,5};
    static uint8_t num_old = 0;
    uint8_t step_loc = 0;
    int8_t dir = 1;
    for(step_loc = 0;step_loc<6;step_loc ++){
        if(step[step_loc] == dir_in){
            break;
        }
    }
    if(step_loc == 0){
        if(num_old == 1){
            dir = 1;
        }else if(num_old == 5){
            dir = -1;
        }
    }
    else if(step_loc  == 5){
        if(num_old == 0) dir = 1;
        else if(num_old == 4) dir = -1;
    }
    else if(step_loc>num_old){
        dir = -1;
    }else if(step_loc<num_old){
        dir = 1;
    }
    num_old = step_loc;
    motor_drive.speed_group[count-1]*=dir;
}
static void update_motor_speed(uint8_t dir_in,uint32_t time){
    int speed_temp = 0;
    static int flag = 0;
    float f = 0;
    if(time == 0){
        motor_drive.speed_group[count++] = 0;
    }else{
        f = (1.0f/(84000000.0f/128)*time);
        f = (1.0f/12.0f)/(f/60.0f);
        motor_drive.speed_group[count++] = f;
    }
    update_speed_dir(dir_in);
    if(count>=SPEED_FILTER_NUM){
        flag = 1;
        count = 0;
    }
    speed_temp = 0;

    if(flag){
        for(uint8_t c = 0;c<SPEED_FILTER_NUM;c++){
            speed_temp += motor_drive.speed_group[c];
        }
        motor_drive.speed = speed_temp / SPEED_FILTER_NUM;
    }else {
        for(uint8_t c = 0;c<count;c++){
            speed_temp += motor_drive.speed_group[c];
        }
        motor_drive.speed = speed_temp / count;
    }
}
float get_motor_speed(void){
    return motor_drive.speed;
}
static void motor_uhvl(void){
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, bldcm_pulse);                            // 通道 1 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_Port, MOTOR_OCNPWM1_Pin, GPIO_PIN_RESET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);                            // 通道 2 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_Port, MOTOR_OCNPWM2_Pin, GPIO_PIN_SET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);                  // 通道 3 配置的占空比
    HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_Port, MOTOR_OCNPWM3_Pin, GPIO_PIN_RESET);      // �??启下桥臂
}
static void motor_uhwl(void){
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, bldcm_pulse);                            // 通道 1 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_Port, MOTOR_OCNPWM1_Pin, GPIO_PIN_RESET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);                            // 通道 2 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_Port, MOTOR_OCNPWM2_Pin, GPIO_PIN_RESET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);                  // 通道 3 配置的占空比
    HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_Port, MOTOR_OCNPWM3_Pin, GPIO_PIN_SET);      // �??启下桥臂
}
static void motor_vhul(void){
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);                            // 通道 1 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_Port, MOTOR_OCNPWM1_Pin, GPIO_PIN_SET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, bldcm_pulse);                            // 通道 2 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_Port, MOTOR_OCNPWM2_Pin, GPIO_PIN_RESET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);                  // 通道 3 配置的占空比
    HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_Port, MOTOR_OCNPWM3_Pin, GPIO_PIN_RESET);      // �??启下桥臂
}
static void motor_vhwl(void){
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);                            // 通道 1 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_Port, MOTOR_OCNPWM1_Pin, GPIO_PIN_RESET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, bldcm_pulse);                            // 通道 2 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_Port, MOTOR_OCNPWM2_Pin, GPIO_PIN_RESET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);                  // 通道 3 配置的占空比
    HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_Port, MOTOR_OCNPWM3_Pin, GPIO_PIN_SET);      // �??启下桥臂
}
static void motor_whul(void){
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);                            // 通道 1 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_Port, MOTOR_OCNPWM1_Pin, GPIO_PIN_SET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);                            // 通道 2 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_Port, MOTOR_OCNPWM2_Pin, GPIO_PIN_RESET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, bldcm_pulse);                  // 通道 3 配置的占空比
    HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_Port, MOTOR_OCNPWM3_Pin, GPIO_PIN_RESET);      // �??启下桥臂
}
static void motor_whvl(void){
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);                            // 通道 1 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_Port, MOTOR_OCNPWM1_Pin, GPIO_PIN_RESET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);                            // 通道 2 配置�?? 0
    HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_Port, MOTOR_OCNPWM2_Pin, GPIO_PIN_SET);    // 关闭下桥�??

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, bldcm_pulse);                  // 通道 3 配置的占空比
    HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_Port, MOTOR_OCNPWM3_Pin, GPIO_PIN_RESET);      // �??启下桥臂
}
void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim){
    if(htim->Instance == TIM3){
        uint8_t  step = 0;
        step = get_hall_state();
        update_motor_speed(step, __HAL_TIM_GET_COMPARE(&htim3,TIM_CHANNEL_1));
        motor_drive.timeout = 0x00;
        if(get_bldcm_direction() == MOTOR_FWD){
            switch (step) {
                case 6:
                    motor_whul();
                    break;
                case 4:
                    motor_whvl();
                    break;
                case 5:
                    motor_uhvl();
                    break;
                case 1:
                    motor_uhwl();
                    break;
                case 3:
                    motor_vhwl();
                    break;
                case 2:
                    motor_vhul();
                    break;
            }
        }
        else {
            switch (step) {
                case 6:
                    motor_uhwl();
                    break;
                case 2:
                    motor_uhvl();
                    break;
                case 3:
                    motor_whvl();
                    break;
                case 1:
                    motor_whul();
                    break;
                case 5:
                    motor_vhul();
                    break;
                case 4:
                    motor_vhwl();
                    break;
            }
        }
        HAL_TIM_GenerateEvent(&htim1,TIM_EVENTSOURCE_COM);
        update_motor_tim = 0;
    }
}

/* USER CODE END 1 */
